package com.wisnews.config;

import com.wisnews.pojo.sec.entity.ViewController;
import com.wisnews.service.sec.IViewControllerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @Description：WebMVC配置
 * @Author: yuyd
 * @Email: yyd8358@foxmail.com
 * @Date: 2020/12/17 15:15
 * @Version 1.0
 */

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

	@Autowired
	private IViewControllerService viewControllerService;

	@Value("${custom.parameter.file-upload-path}")
	private String upPath;

	/**
	 * @description: 配置视图映射
	 * @param:
	 * @return:
	 * @author yuyd
	 * @date: 2021/5/2 22:16
	 */
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {

		List<ViewController> viewControllers = viewControllerService.list();

		viewControllers.forEach(viewController -> {
			registry.addViewController( viewController.getUrlPath() )
					.setViewName(viewController.getViewName());
		});
	}

	/**
	 * @description: 配置虚拟路径映射，解决上传文件后在服务器内部，无法映射问题。前台无法回显
	 * @param:
	 * @return:
	 * @author yuyd
	 * @date: 2021/5/2 22:16
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {

		new RestTemplate();
		registry.addResourceHandler("/newsImg/**").addResourceLocations("file:/"+upPath+"/");
		WebMvcConfigurer.super.addResourceHandlers(registry);
	}



}
